package scriptedsands;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ScriptedSandsGUI.java
 *
 * Created on Sep 27, 2010, 1:48:29 AM
 */
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ListSelectionEvent;

/**
 *
 * @author Dalton
 */
public abstract class AbstractGUI extends javax.swing.JFrame {

    private AbstractEngine engine;

    /** Creates new form ScriptedSandsGUI */
    public AbstractGUI() {
        initComponents();
        setup();
    }

    public void setup() {
        jSliderPenSize.setMinimum(1);
        jSliderPenSize.setMaximum(128);
        jSliderPenSize.setValue(1);
    }

    public void setEngine(AbstractEngine e) {
        engine = e;
    }

    public void start() {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                setVisible(true);
            }
        });

        Timer sandTimer = new Timer();
        sandTimer.scheduleAtFixedRate( new TimerTask() {
                public void run() {
                        repaint();
                }
        }, 0, 1000/30 );
    }

    public class Sandbox extends JPanel {

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(engine.getImage(), 0, 0, null);
        }
    }

    public class JLabelFPS extends JLabel {

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            setText(String.valueOf(engine.getFPS()));
        }
    }

    public class JComponentContainer {
        private JComponent component;
    }

   public class JTabbedPaneControls extends JTabbedPane {
       public HashMap<String, JComponentContainer>
               components = new HashMap<String, JComponentContainer>();
   }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        jPanelSandbox = new Sandbox();
        jTabbedPaneControls = new JTabbedPaneControls();
        jPanelElements = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListItems = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        jListGroup = new javax.swing.JList();
        jLabelElements = new javax.swing.JLabel();
        jLabelGroup = new javax.swing.JLabel();
        jSliderPenSize = new javax.swing.JSlider();
        jLabelPenSize = new javax.swing.JLabel();
        jLabelFPS = new JLabelFPS();
        jMenuBar2 = new javax.swing.JMenuBar();
        jMenu3 = new javax.swing.JMenu();
        jMenuItemNewSandbox = new javax.swing.JMenuItem();
        jMenuItemNewGUI = new javax.swing.JMenuItem();
        jMenuItemNewEngine = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();

        jMenuBar1.setName("jMenuBar1"); // NOI18N

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("scriptedsands/Bundle"); // NOI18N
        jMenu1.setText(bundle.getString("AbstractGUI.jMenu1.text")); // NOI18N
        jMenu1.setName("jMenu1"); // NOI18N
        jMenuBar1.add(jMenu1);

        jMenu2.setText(bundle.getString("AbstractGUI.jMenu2.text")); // NOI18N
        jMenu2.setName("jMenu2"); // NOI18N
        jMenuBar1.add(jMenu2);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N
        setResizable(false);

        jPanelSandbox.setName("jPanelSandbox"); // NOI18N
        jPanelSandbox.setPreferredSize(new java.awt.Dimension(450, 450));
        jPanelSandbox.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jPanelSandboxMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jPanelSandboxMouseReleased(evt);
            }
        });
        jPanelSandbox.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                jPanelSandboxMouseDragged(evt);
            }
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                jPanelSandboxMouseMoved(evt);
            }
        });

        javax.swing.GroupLayout jPanelSandboxLayout = new javax.swing.GroupLayout(jPanelSandbox);
        jPanelSandbox.setLayout(jPanelSandboxLayout);
        jPanelSandboxLayout.setHorizontalGroup(
            jPanelSandboxLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 450, Short.MAX_VALUE)
        );
        jPanelSandboxLayout.setVerticalGroup(
            jPanelSandboxLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 450, Short.MAX_VALUE)
        );

        jTabbedPaneControls.setName("jTabbedPaneControls"); // NOI18N

        jPanelElements.setName("jPanelElements"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jListItems.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jListItems.setName("jListItems"); // NOI18N
        jListItems.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jListItemsValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jListItems);

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jListGroup.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jListGroup.setName("jListGroup"); // NOI18N
        jListGroup.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jListGroupValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(jListGroup);

        jLabelElements.setText(bundle.getString("AbstractGUI.jLabelElements.text")); // NOI18N
        jLabelElements.setName("jLabelElements"); // NOI18N

        jLabelGroup.setText(bundle.getString("AbstractGUI.jLabelGroup.text")); // NOI18N
        jLabelGroup.setName("jLabelGroup"); // NOI18N

        javax.swing.GroupLayout jPanelElementsLayout = new javax.swing.GroupLayout(jPanelElements);
        jPanelElements.setLayout(jPanelElementsLayout);
        jPanelElementsLayout.setHorizontalGroup(
            jPanelElementsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelElementsLayout.createSequentialGroup()
                .addGroup(jPanelElementsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelGroup)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanelElementsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanelElementsLayout.createSequentialGroup()
                        .addComponent(jLabelElements)
                        .addGap(142, 142, 142))
                    .addGroup(jPanelElementsLayout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE)
                        .addContainerGap())))
        );
        jPanelElementsLayout.setVerticalGroup(
            jPanelElementsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelElementsLayout.createSequentialGroup()
                .addGroup(jPanelElementsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelElements)
                    .addComponent(jLabelGroup))
                .addGap(6, 6, 6)
                .addGroup(jPanelElementsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 405, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 405, Short.MAX_VALUE)))
        );

        jTabbedPaneControls.addTab(bundle.getString("AbstractGUI.jPanelElements.TabConstraints.tabTitle"), jPanelElements); // NOI18N

        jSliderPenSize.setName("jSliderPenSize"); // NOI18N
        jSliderPenSize.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSliderPenSizeStateChanged(evt);
            }
        });

        jLabelPenSize.setText(bundle.getString("AbstractGUI.jLabelPenSize.text")); // NOI18N
        jLabelPenSize.setName("jLabelPenSize"); // NOI18N

        jLabelFPS.setText(bundle.getString("AbstractGUI.jLabelFPS.text")); // NOI18N
        jLabelFPS.setName("jLabelFPS"); // NOI18N

        jMenuBar2.setName("jMenuBar2"); // NOI18N

        jMenu3.setText(bundle.getString("AbstractGUI.jMenu3.text")); // NOI18N
        jMenu3.setName("jMenu3"); // NOI18N

        jMenuItemNewSandbox.setText(bundle.getString("AbstractGUI.jMenuItemNewSandbox.text")); // NOI18N
        jMenuItemNewSandbox.setName("jMenuItemNewSandbox"); // NOI18N
        jMenu3.add(jMenuItemNewSandbox);

        jMenuItemNewGUI.setText(bundle.getString("AbstractGUI.jMenuItemNewGUI.text")); // NOI18N
        jMenuItemNewGUI.setName("jMenuItemNewGUI"); // NOI18N
        jMenu3.add(jMenuItemNewGUI);

        jMenuItemNewEngine.setText(bundle.getString("AbstractGUI.jMenuItemNewEngine.text")); // NOI18N
        jMenuItemNewEngine.setName("jMenuItemNewEngine"); // NOI18N
        jMenu3.add(jMenuItemNewEngine);

        jSeparator1.setName("jSeparator1"); // NOI18N
        jMenu3.add(jSeparator1);

        jMenuItem4.setText(bundle.getString("AbstractGUI.jMenuItem4.text")); // NOI18N
        jMenuItem4.setName("jMenuItem4"); // NOI18N
        jMenu3.add(jMenuItem4);

        jMenuBar2.add(jMenu3);

        jMenu4.setText(bundle.getString("AbstractGUI.jMenu4.text")); // NOI18N
        jMenu4.setName("jMenu4"); // NOI18N
        jMenuBar2.add(jMenu4);

        setJMenuBar(jMenuBar2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanelSandbox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabelFPS, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabelPenSize)
                        .addContainerGap())
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jSliderPenSize, javax.swing.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jTabbedPaneControls, javax.swing.GroupLayout.DEFAULT_SIZE, 374, Short.MAX_VALUE)
                            .addContainerGap()))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jTabbedPaneControls, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 450, Short.MAX_VALUE)
                    .addComponent(jPanelSandbox, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(148, 148, 148)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabelPenSize)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSliderPenSize, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabelFPS)
                        .addGap(11, 11, 11))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jPanelSandboxMousePressed(MouseEvent evt) {//GEN-FIRST:event_jPanelSandboxMousePressed
        engine.drawing(true);
        engine.updateMousePos(evt.getX(), evt.getY());
        engine.draw();
    }//GEN-LAST:event_jPanelSandboxMousePressed

    private void jPanelSandboxMouseReleased(MouseEvent evt) {//GEN-FIRST:event_jPanelSandboxMouseReleased
        engine.drawing(false);
    }//GEN-LAST:event_jPanelSandboxMouseReleased

    private void jSliderPenSizeStateChanged(ChangeEvent evt) {//GEN-FIRST:event_jSliderPenSizeStateChanged
        if (engine != null)
            engine.setPenSize(jSliderPenSize.getValue());
        jLabelPenSize.setText("Pen Size: " + jSliderPenSize.getValue());
    }//GEN-LAST:event_jSliderPenSizeStateChanged

    private void jPanelSandboxMouseDragged(MouseEvent evt) {//GEN-FIRST:event_jPanelSandboxMouseDragged
        engine.updateMousePos(evt.getX(), evt.getY());
        engine.draw();
}//GEN-LAST:event_jPanelSandboxMouseDragged

    private void jListItemsValueChanged(ListSelectionEvent evt) {//GEN-FIRST:event_jListItemsValueChanged
        engine.setElement(jListItems.getSelectedValue());
}//GEN-LAST:event_jListItemsValueChanged

    private void jPanelSandboxMouseMoved(MouseEvent evt) {//GEN-FIRST:event_jPanelSandboxMouseMoved
        engine.updateMousePos(evt.getX(), evt.getY());
    }//GEN-LAST:event_jPanelSandboxMouseMoved

    private void jListGroupValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jListGroupValueChanged
        //jListItems.setListData(j);
    }//GEN-LAST:event_jListGroupValueChanged

    /**
    * @param args the command line arguments
    */
    /*public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void start() {
                new ScriptedSandsGUI().setVisible(true);
            }
        });
    }*/

    // Variables declaration - do not modify//GEN-BEGIN:variables
    protected javax.swing.JLabel jLabelElements;
    protected javax.swing.JLabel jLabelFPS;
    protected javax.swing.JLabel jLabelGroup;
    protected javax.swing.JLabel jLabelPenSize;
    protected javax.swing.JList jListGroup;
    protected javax.swing.JList jListItems;
    protected javax.swing.JMenu jMenu1;
    protected javax.swing.JMenu jMenu2;
    protected javax.swing.JMenu jMenu3;
    protected javax.swing.JMenu jMenu4;
    protected javax.swing.JMenuBar jMenuBar1;
    protected javax.swing.JMenuBar jMenuBar2;
    protected javax.swing.JMenuItem jMenuItem4;
    protected javax.swing.JMenuItem jMenuItemNewEngine;
    protected javax.swing.JMenuItem jMenuItemNewGUI;
    protected javax.swing.JMenuItem jMenuItemNewSandbox;
    protected javax.swing.JPanel jPanelElements;
    protected javax.swing.JPanel jPanelSandbox;
    protected javax.swing.JScrollPane jScrollPane1;
    protected javax.swing.JScrollPane jScrollPane2;
    protected javax.swing.JPopupMenu.Separator jSeparator1;
    protected javax.swing.JSlider jSliderPenSize;
    protected javax.swing.JTabbedPane jTabbedPaneControls;
    // End of variables declaration//GEN-END:variables

}
